from sqlalchemy import (
    Column,
    Integer,
    String,
    Date,
    UniqueConstraint,
    DECIMAL,
    Float,
    Text,
    Time,
)
from .database import Base


class Workday(Base):
    __tablename__ = "workday"

    trade_date = Column(Date, primary_key=True)  # 交易日，主键


class StockInfo(Base):
    __tablename__ = "stock_info"
    __table_args__ = (UniqueConstraint("symbol", name="idx_symbol_unique"),)

    symbol = Column(String(20), primary_key=True)  # Stock code ("代码")
    name = Column(String(255))  # Stock name ("名称")
    market_cap = Column(DECIMAL(20, 2))  # Total market cap ("总市值")
    circulating_cap = Column(DECIMAL(20, 2))  # Circulating market cap ("流通市值")
    pe_ratio_dynamic = Column(DECIMAL(10, 2))  # Dynamic P/E ratio ("市盈率-动态")
    pb_ratio = Column(DECIMAL(10, 2))  # P/B ratio ("市净率")
    sector = Column(String(255))  # sector ("板块")
    # area = Column(String(255))  # area ("地区")
    # industry = Column(String(255))  # industry ("行业")
    listing_date = Column(Date)  # Listing date ("上市时间")
    total_shares = Column(DECIMAL(20, 2))  # Total shares ("总股本")

    def __repr__(self):
        return f"<StockInfo(symbol={self.symbol}, name={self.name}, market_cap={self.market_cap}, circulating_cap={self.circulating_cap}, pe_ratio_dynamic={self.pe_ratio_dynamic}, pb_ratio={self.pb_ratio}, sector={self.sector}, listing_date={self.listing_date}, total_shares={self.total_shares})>"


class StockDaily(Base):
    __tablename__ = "stock_daily"

    symbol = Column(String(20), primary_key=True)  # 股票代码
    date = Column(Date, primary_key=True)  # 交易日
    open = Column(Float)  # 开盘价
    high = Column(Float)  # 最高价
    low = Column(Float)  # 最低价
    close = Column(Float)  # 收盘价
    volume = Column(Float)  # 成交量 (单位: 股)
    amount = Column(Float)  # 成交额 (单位: 元)
    outstanding_share = Column(Float)  # 流动股本 (单位: 股)
    turnover = Column(Float)  # 换手率
    amplitude = Column(Float)  # 振幅 (单位: %)
    price_change_rate = Column(Float)  # 涨跌幅 (单位: %)
    price_change_amount = Column(Float)  # 涨跌额 (单位: 元)
    main_net_inflow_amount = Column(Float)  # 主力净流入-净额
    main_net_inflow_ratio = Column(Float)  # 主力净流入-净占比
    super_large_net_inflow_amount = Column(Float)  # 超大单净流入-净额
    super_large_net_inflow_ratio = Column(Float)  # 超大单净流入-净占比
    large_net_inflow_amount = Column(Float)  # 大单净流入-净额
    large_net_inflow_ratio = Column(Float)  # 大单净流入-净占比
    medium_net_inflow_amount = Column(Float)  # 中单净流入-净额
    medium_net_inflow_ratio = Column(Float)  # 中单净流入-净占比
    small_net_inflow_amount = Column(Float)  # 小单净流入-净额
    small_net_inflow_ratio = Column(Float)  # 小单净流入-净占比
    buyer_institution_count = Column(Float)  # 买方机构数
    seller_institution_count = Column(Float)  # 卖方机构数
    institution_buy_amount = Column(Float)  # 机构买入总额 (单位: 元)
    institution_sell_amount = Column(Float)  # 机构卖出总额 (单位: 元)
    institution_net_buy_amount = Column(Float)  # 机构买入净额 (单位: 元)
    total_market_turnover = Column(Float)  # 市场总成交额 (单位: 元)
    institution_net_buy_ratio = Column(Float)  # 机构净买额占总成交额比
    lhb_reason = Column(Text)  # 上榜原因


class SectorDaily(Base):
    __tablename__ = "sector_daily"

    name = Column(String(255), primary_key=True)  # 行业板块名称
    date = Column(Date, primary_key=True)  # 日期 (格式 YYYYMMDD)

    main_net_inflow = Column(Float)  # 主力净流入-净额
    main_net_ratio = Column(Float)  # 主力净流入-净占比 (%)

    huge_net_inflow = Column(Float)  # 超大单净流入-净额
    huge_net_ratio = Column(Float)  # 超大单净流入-净占比 (%)

    large_net_inflow = Column(Float)  # 大单净流入-净额
    large_net_ratio = Column(Float)  # 大单净流入-净占比 (%)

    medium_net_inflow = Column(Float)  # 中单净流入-净额
    medium_net_ratio = Column(Float)  # 中单净流入-净占比 (%)

    small_net_inflow = Column(Float)  # 小单净流入-净额
    small_net_ratio = Column(Float)  # 小单净流入-净占比 (%)


class MarketDaily(Base):
    __tablename__ = "market_daily"

    date = Column(Date, primary_key=True)  # 日期 (格式 YYYYMMDD)

    sh_close = Column(Float)  # 上证-收盘价
    sh_rate = Column(Float)  # 上证-涨跌幅
    sz_close = Column(Float)  # 深证-收盘价
    sz_rate = Column(Float)  # 深证-涨跌幅

    main_net_inflow = Column(Float)  # 主力净流入-净额
    main_net_ratio = Column(Float)  # 主力净流入-净占比 (%)

    huge_net_inflow = Column(Float)  # 超大单净流入-净额
    huge_net_ratio = Column(Float)  # 超大单净流入-净占比 (%)

    large_net_inflow = Column(Float)  # 大单净流入-净额
    large_net_ratio = Column(Float)  # 大单净流入-净占比 (%)

    medium_net_inflow = Column(Float)  # 中单净流入-净额
    medium_net_ratio = Column(Float)  # 中单净流入-净占比 (%)

    small_net_inflow = Column(Float)  # 小单净流入-净额
    small_net_ratio = Column(Float)  # 小单净流入-净占比 (%)


class StockDailyLimitUp(Base):
    __tablename__ = "stock_daily_limit_up"

    symbol = Column(String(20), primary_key=True)  # 股票代码
    name = Column(String(20))  # 股票名称
    date = Column(Date, primary_key=True)  # 交易日
    percentage_change = Column(Float)  # 涨跌幅 (%)
    latest_price = Column(Float)  # 最新价
    amount = Column(Float)  # 成交额
    circulating_market_value = Column(Float)  # 流通市值
    total_market_value = Column(Float)  # 总市值
    amount_rate = Column(Float)  # 换手率 (%)
    board_funds = Column(Float)  # 封板资金
    first_board_time = Column(Time)  # 首次封板时间 (格式: 09:25:00)
    last_board_time = Column(Time)  # 最后封板时间 (格式: 09:25:00)
    board_break_count = Column(Integer)  # 炸板次数
    limit_up_statistics = Column(String(20))  # 涨停统计
    consecutive_boards = Column(Integer)  # 连板数 (1 为首板)
    sector = Column(String(64))  # 所属行业


class StockDailyLongHuBang(Base):
    __tablename__ = "stock_daily_longhubang"

    # Define columns based on the provided structure
    symbol = Column(String(10), primary_key=True)  # 代码
    name = Column(String(50))  # 名称
    date = Column(Date, primary_key=True)  # 上榜日
    analysis = Column(Text)  # 解读
    closing_price = Column(Float)  # 收盘价
    percentage_change = Column(Float)  # 涨跌幅
    net_buy_amount = Column(Float)  # 龙虎榜净买额
    buy_amount = Column(Float)  # 龙虎榜买入额
    sell_amount = Column(Float)  # 龙虎榜卖出额
    longhubang_amount = Column(Float)  # 龙虎榜成交额
    market_total_amount = Column(Float)  # 市场总成交额
    net_buy_ratio = Column(Float)  # 净买额占总成交比
    amount_ratio = Column(Float)  # 成交额占总成交比
    amount_rate = Column(Float)  # 换手率
    circulating_market_value = Column(Float)  # 流通市值
    reason_for_listing = Column(String(100))  # 上榜原因
    after_1_day = Column(Float)  # 上榜后1日
    after_2_days = Column(Float)  # 上榜后2日
    after_5_days = Column(Float)  # 上榜后5日
    after_10_days = Column(Float)  # 上榜后10日

    def __repr__(self):
        return (
            f"<StockDailyLonghubang(code={self.code}, listed_date={self.listed_date})>"
        )
